import Vue from 'vue';
import vueResource from 'vue-resource'

Vue.use(vueResource);
const API_ROOT = '/api';
// const API_ROOT = '//api.sikutech.com/api';

function fetch(service, data, loading = false, that, method = 'get') {
    //return Vue.http.get(API_ROOT + service, data)
    return getData(service, data, loading, that, method)
}

async function getData(service, data, loading, that, method) {
    if (loading) {
        that.loading = true
    }
    return new Promise((resolve, reject) => {

        if (method === 'post') {
            if (service === '/user/info' || service === '/micro/jsapi') {
                Vue.http.options.emulateJSON = true;
                Vue.http.options.headers = {
                    'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
                }
            } else if (service === '/category/add') {
                Vue.http.options.headers = {
                    'Content-Type': 'multipart/form-data'
                }
            } else {
                Vue.http.options.emulateJSON = false;
                Vue.http.options.headers = {
                    'Content-Type': 'application/json'
                }
            }
        }
        console.log(Vue.http.options);
        if (method === 'post') {
            Vue.http.post(API_ROOT + service, data).then(response => {
                that.loading = false;
                console.log(response);
                if (response.status === 200) {
                    let data = response.body;
                    if (data.errcode === 0) {
                        resolve(data.data);
                        return
                    } else {
                        that.toast = true;
                        that.toastText = data.errmsg
                    }
                }
                reject(data)
            }, error => {
                that.toastText = (error.body.message);
                that.toast = true;
                that.loading = false
            })
        } else {
            Vue.http.get(API_ROOT + service, data).then(response => {
                that.loading = false;
                console.log(response);
                if (response.status === 200) {
                    let data = response.body;
                    if (data.errcode === 0) {
                        resolve(data.data);
                        return
                    } else {
                        that.toast = true;
                        that.toastText = data.errmsg
                    }
                }
                reject(data)
            }, error => {
                that.toastText = (error.body.message);
                that.toast = true;
                that.loading = false
            })
        }
    })
}

export function GetUser(data, that) {
    return fetch('/user/info', data, true, that, 'post');
}

export function GetCategory(data, that) {
    return fetch('/category/getall', data, true, that);
}

export function GetMicro(data, that) {
    return fetch('/micro/list', data, true, that, 'post');
}

export function addMicro(data, that) {
    return fetch('/micro/add_category', data, true, that, 'post');
}

export function deleteMicro(data, that) {
    return fetch('/micro/delete_category', data, true, that, 'post');
}

export function deleteCate(data, that) {
    return fetch('/category/delete', data, true, that, 'post');
}

export function addCate(data, that) {
    return fetch('/category/add', data, true, that, 'post');
}

export function getConfig(data, that) {
    return fetch('/micro/jsapi', data, false, that, 'post');
}
